package com.hourses.service.impl;

import com.hourses.mapper.ProductMapper;
import com.hourses.page.Page;
import com.hourses.pojo.Product;
import com.hourses.pojo.Result;
import com.hourses.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/5 16:32
 */
@Service
public class ProductServiceImpl implements ProductService {

    //查询出满足条件的商品总数
    @Autowired
    private ProductMapper productMapper;
    @Override
    public Integer selectProductPageList(Product product) {
        Integer count = productMapper.selectProductPageList(product);
        return count;
    }

    //查询出每一页需要展示的商品列表
    @Override
    public List<Product> selectChildList(Product product, Page page) {
        List<Product> products = productMapper.selectChildList(product, page);
        return products;
    }

    //检查要添加的商品是否已经存在于数据库中，要求：商品名称和编号至少要有一个不同
    @Override
    public Integer selectAddProductIsExist(Product product) {
        Integer count = productMapper.selectAddProductIsExist(product);
        return count;
    }

    //添加商品
    @Override
    public Result addProduct(Product product) {
        Integer count = productMapper.addProduct(product);
        if (count>0){
            return Result.ok("添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"添加失败！请联系管理员...");
    }

    //修改商品上下架状态
    @Override
    public Result changeUpDownState(Product product) {
        Integer count = productMapper.changeUpDownState(product);
        if (count>0){
            if (product.getUpDownState().equals("1")){
                return Result.ok("上架成功！");
            }else {
                return Result.ok("下架成功！");
            }
        }
        if(product.getUpDownState().equals("1")){
            return Result.err(Result.CODE_ERR_BUSINESS,"上架失败！请联系管理员...");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"下架失败！请联系管理员...");
    }

    //删除商品
    @Override
    public Result deleteProduct(List<Integer> productIds) {
        Integer count = productMapper.deleteProduct(productIds);
        if (count>0){
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！请联系管理员...");
    }

    //根据id查找商品
    @Override
    public List<Product> selectProductById(List<Integer> productId) {
        List<Product> products = productMapper.selectProductById(productId);
        return products;
    }

    //根据productName和productNum查询商品
    @Override
    public List<Product> selectByNameAndNum(Product product) {
        List<Product> products = productMapper.selectByNameAndNum(product);
        return products;
    }

    //修改商品信息
    @Override
    public Result updateProduct(Product product) {
        Integer count = productMapper.updateProduct(product);
        if (count>0){
            return Result.ok("修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改失败！请联系管理员...");
    }


}
